package com.love.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.love.cloud.admin.api.entity.SysDept;
import com.love.cloud.admin.api.entity.SysUser;
import com.love.cloud.admin.api.vo.LocalDeptInfoVo;
import com.love.cloud.admin.api.vo.LocalUserInfoVo;
import com.love.cloud.admin.api.vo.LocalUserTreeVo;
import com.love.cloud.admin.config.properties.SosUserBaseProperties;
import com.love.cloud.admin.dto.LocalUserInfoDto;
import com.love.cloud.admin.dto.LocalUserTreeDto;
import com.love.cloud.admin.dto.SysUserDeptDto;
import com.love.cloud.admin.mapper.SysDeptMapper;
import com.love.cloud.admin.mapper.SysUserDeptMapper;
import com.love.cloud.admin.service.SelectDataService;
import com.love.cloud.admin.service.SysUserDeptService;
import com.love.cloud.admin.service.SysUserService;
import com.love.cloud.common.core.util.ModelMapperUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author hongdongqu
 */
@Service
public class SelectDataServiceImpl implements SelectDataService {
    @Resource
    private SosUserBaseProperties sosUserBaseProperties;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private SysUserService userService;
    @Resource
    private ModelMapperUtils modelMapperUtils;
    @Resource
    private SysUserDeptService sysUserDeptService;
    @Resource
    private SysUserDeptMapper sysUserDeptMapper;

    @Override
    public LocalUserTreeDto getDeptAndUserInfo(LocalUserTreeVo localUserTreeVo) {
        //组织编码
        String orgCode="";
        if(StringUtils.isEmpty(localUserTreeVo.getOrgCode())){
            orgCode=sosUserBaseProperties.getRootOrgCode();
            //直接设置限制限制orgCode
            if(StringUtils.isNotEmpty(localUserTreeVo.getCascadeOrganizationCode())){
                orgCode=localUserTreeVo.getCascadeOrganizationCode();
            }
        }else{
            orgCode=localUserTreeVo.getOrgCode();
        }
        String type="all";
        if(StringUtils.isNotEmpty(localUserTreeVo.getType())){
            type=localUserTreeVo.getType();
        }
        LocalUserTreeDto localUserTreeDto=new LocalUserTreeDto();
        localUserTreeDto.setOrgCode(orgCode);
        QueryWrapper<SysDept> deptQueryWrapper=new QueryWrapper<>();
        deptQueryWrapper.lambda().eq(SysDept::getId,orgCode)
                .like(StringUtils.isNotEmpty(localUserTreeVo.getCascadeOrganizationCode()), SysDept::getPath,localUserTreeVo.getCascadeOrganizationCode());
        SysDept sysDept = sysDeptMapper.selectOne(deptQueryWrapper);
        //如果无法获取到当前部门的信息就无法获取到下级组织和人员信息
        if(sysDept!=null){
            //先获取部门列表
            if(type.contains("all")||type.contains("org")){
                QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
                queryWrapper.lambda().eq(SysDept::getParentId,orgCode);
                List<SysDept> sysDeptList = sysDeptMapper.selectList(queryWrapper);
                localUserTreeDto.setOrgList(sysDeptList);
            }
            if(type.contains("all")||type.contains("user")){
                localUserTreeDto.setOrgName(sysDept.getName());
                localUserTreeDto.setOrgInfo(sysDept);

                List<SysUser> userListDeptList = userService.getUserListDeptId(orgCode,localUserTreeVo.getPosition());
                //再获取人员列表
                localUserTreeDto.setUserList(userListDeptList);
            }
        }
        return localUserTreeDto;
    }

    @Override
    public List<LocalUserInfoDto> getUserInfo(LocalUserInfoVo localUserInfoVo) {
        List<LocalUserInfoDto> userInfoDtoList=new ArrayList<>();
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().nested(qw->qw.like(SysUser::getMobile,localUserInfoVo.getNameKeywords()).or()
                .like(SysUser::getName,localUserInfoVo.getNameKeywords()).or().like(SysUser::getFirstSpell,localUserInfoVo.getNameKeywords())
                .or().like(SysUser::getSpell,localUserInfoVo.getNameKeywords())).eq(StringUtils.isNotEmpty(localUserInfoVo.getPosition()), SysUser::getPosition,localUserInfoVo.getPosition());
        List<SysUser> list = userService.list(queryWrapper);
        Set<Long> idSet = list.stream().map(SysUser::getId).collect(Collectors.toSet());
        //符合部门的信息
        if(idSet!=null&&idSet.size()>0){
            QueryWrapper<SysDept> queryWrapper2=new QueryWrapper();
            queryWrapper2.in("t2.user_id",idSet)
                    .like(StringUtils.isNotEmpty(localUserInfoVo.getCascadeOrganizationCode()),"t.path",localUserInfoVo.getCascadeOrganizationCode());
            List<SysUserDeptDto> sysUserDeptList = this.sysUserDeptMapper.getUserDeptList(queryWrapper2);
            Map<Long, List<SysUserDeptDto>> userDeptGroup = sysUserDeptList.stream().collect(Collectors.groupingBy(SysUserDeptDto::getUserId));
            List<LocalUserInfoDto> userInfoList = modelMapperUtils.toNewModelList(list, LocalUserInfoDto.class);
            for (LocalUserInfoDto item:userInfoList) {
                List<SysUserDeptDto> userDeptDtoList = userDeptGroup.get(item.getId());
                if(userDeptDtoList!=null&&userDeptDtoList.size()>0){
                    item.setOrgCode(userDeptDtoList.get(0).getName());
                    item.setOrgName(userDeptDtoList.get(0).getDeptId().toString());
                    userInfoDtoList.add(item);
                }
            }
        }
        return userInfoDtoList;
    }

    @Override
    public List<SysDept> searchLocalDeptInfo(LocalDeptInfoVo localDeptInfoVo) {
        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().nested(qw->qw.like(SysDept::getName,localDeptInfoVo.getNameKeywords())
                .or().like(SysDept::getFirstSpell,localDeptInfoVo.getNameKeywords())
                .or().like(SysDept::getSpell,localDeptInfoVo.getNameKeywords()))
                .like(StringUtils.isNotEmpty(localDeptInfoVo.getCascadeOrganizationCode()), SysDept::getPath,localDeptInfoVo.getCascadeOrganizationCode());
        List<SysDept> sysDeptList = this.sysDeptMapper.selectList(queryWrapper);
        return sysDeptList;
    }

    @Override
    public Object getOrganizationLine(String organizationCode) {
        return "还未实现";
    }



}
